(function (line) {
    line.define('line.client.Router', null, {
        singleton:true,
        events:['hashChange'],
        properties:{

        },
        methods:{
            init:function () {
                var self = this;
                window.onhashchange = function () {
                    self._onHashChange();
                };

                this._routes = [];
            },
            navigate:function (url) {
                location.hash = url;
            },
            mapRoute:function (path, action, controller) {
                this._routes.push({
                    path:path,
                    action:action,
                    controller:controller
                })
            },
            _parseHash:function (hash) {
                var tokens = hash.replace('#', '').split('/');
                var controller = tokens[0] || 'home',
                    action = tokens[1] || 'index',
                    params = tokens[2];

                return{
                    controller:controller,
                    action:action,
                    params:params
                };
            },
            _onHashChange:function () {
                this.fire('hashChange');
                this._route();
            },
            _route:function () {
                var hash = this._currentHash = location.hash;
                var route = this._parseHash(hash);
                line.client.ControllerResolver.resolve('HomeController',function(controller){
                    if (controller) {
                        controller[route.action](route.param);
                    }
                });
            }
        }
    });
})(line);
